package com.taobaolite.order;

import com.taobaolite.order_has_items.OrderHasItems;
import com.taobaolite.user.guest.Guest;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import org.springframework.context.annotation.DependsOn;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.sql.Timestamp;
import java.util.Set;

@Table(name = "orders")
@Entity
@DependsOn("Guest")
public class Order {
    @Id
    @SequenceGenerator(
            name = "order_id_generator",
            sequenceName = "order_id_sequence",
            initialValue = 7000000, // 7 digits
            allocationSize = 2117
    )
    @GeneratedValue(
            strategy = GenerationType.AUTO,
            generator = "order_id_generator"
    )
    @NotNull
    private Long orderId;
    @NotNull
    private OrderStatus status;
    @NotNull
    private Timestamp time;

    @ManyToOne
    @JoinColumn(name="guestId")
    private Guest guest;

    @OneToMany(cascade = CascadeType.ALL)
    @OnDelete(action = OnDeleteAction.CASCADE)
    @JoinColumn(name = "orderId")
    private Set<OrderHasItems> items;

    public Set<OrderHasItems> getItems() {
        return items;
    }

    public void setItems(Set<OrderHasItems> items) {
        this.items = items;
    }

    public Long getOrderId() {
        return orderId;
    }

    public void setOrderId(Long orderId) {
        this.orderId = orderId;
    }

    public OrderStatus getStatus() {
        return status;
    }

    public void setStatus(OrderStatus status) {
        this.status = status;
    }

    public Timestamp getTime() {
        return time;
    }

    public void setTime(Timestamp time) {
        this.time = time;
    }

    public Guest getGuest() {
        return guest;
    }

    public void setGuest(Guest guest) {
        this.guest = guest;
    }

    public Order() {}

    public Order(OrderBody orderBody, Guest guest) {
        this.orderId = orderBody.getOrderId();
        this.status = orderBody.getStatus();
        this.time = orderBody.getTime();
        this.guest = guest;
    }
    public Order(OrderStatus status, Timestamp time, Guest guest) {
        this.status = status;
        this.time = time;
        this.guest = guest;
    }
}